﻿@{
    ViewData["Title"] = "角色管理";
}
<link rel="stylesheet" href="~/lib/dtree/dtree.css">
<link rel="stylesheet" href="~/lib/dtree/font/dtreefont.css">

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto search" lay-filter="app-content-list">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">角色名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入" maxlength="50" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" id="searchBut" lay-submit="" lay-filter="search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                    </button>
                    <button onclick="add()" class="layui-btn">新增角色</button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
        </div>        
    </div>
</div>

<script type="text/javascript" src="~/lib/dtree/dtree.js"></script>

<script> 
   

    //设置菜单
    $('#m_room').addClass("layui-this");
    $('#m_system').addClass("layui-nav-itemed");


    //监听搜索
    form.on('submit(search)', function (data) {
        var field = data.field;

        //执行重载
        table.reload('LAY-app-content-list', {
            where: field
        });
    });

    $(document).on('keydown', function (e) {  //document为当前元素，限制范围，如果不限制的话会一直有事件
        if (e.keyCode == 13) {
            $("#searchBut").click();
        }
    })


    //加载table
    var tableObj = table.render({
        elem: '#LAY-app-content-list'
        , url: '/role/list'
        , page: true
        , height: 'full-110'
        , cols: [[
            { field: 'title', title: '角色名称', minWidth: 300, templet: '<div>{{ d.roleName}}</div>' },
            { field: 'title', title: '是否只读', minWidth: 300, templet: '<div>{{ getFlag(d.view_flag)}}</div>' }
            , {
                field: 'opr', title: '操作',width:150,
                templet: '<div class="layui-table-cell laytable-cell-11-0-8"> <a class="layui-btn layui-btn-normal layui-btn-xs" onclick="edit(\'{{ d.id }}\')"><i class="layui-icon layui-icon-edit"></i>编辑</a>  <a class="layui-btn layui-btn-danger layui-btn-xs" onclick="deleteItem(\'{{ d.id }}\')"><i class="layui-icon layui-icon-delete"></i>删除</a> </div>'
            }

            ]]
            , skin: 'line'
        });


    function getFlag(flag) {
        if (flag == null) {
            return '否';
        } else {
            return '是';
        }
    }

    //新增角色
    function add() {
        $.get("/role/create", function (str) {
            var index = layer.open({
                type: 1,
                title:'新增角色',
                content: str,
                area:["600px","500px"]
            });

            form.render("", 'createForm');

            //监听提交
            form.on('submit(create)', function (data) {

                var params = dtree.getCheckbarNodesParam("demoTree");
                params = JSON.stringify(params);
                var name = $('#name').val();
                var field = data.field; //获取提交的字段
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/role/doCreate",
                {
                    view_flag: field.view_flag,
                    name: name,
                    nodes: params
                }, function (d) {
                    layer.msg(d.msg);
                    if (d.code == 1) {
                        tableObj.reload();
                        layer.close(index);
                    }
                });
            });
        });
    }

    //编辑角色
    function edit(id) {
        $.get("/role/edit?id="+id, {}, function (str) {
            var index = layer.open({
                type: 1,
                content: str,
                area: ["600px", "500px"]
            });

            form.render("", 'editForm');

            //监听提交
            form.on('submit(create)', function (data) {

                var params = dtree.getCheckbarNodesParam("demoTree");
                params = JSON.stringify(params);
                var name = $('#name').val();
                var field = data.field; //获取提交的字段
                //提交 Ajax 成功后，关闭当前弹层并重载表格
                $.post("/role/doEdit",
                    {
                        id: $('#id').val(),
                        view_flag: field.view_flag,
                        name: name,
                        nodes: params
                    }, function (d) {
                        layer.msg(d.msg);
                        if (d.code == 1) {
                            tableObj.reload();
                            layer.close(index);
                        }
                    });
            });
        });
    }

    function deleteItem(id) {
        layer.confirm("确定要删除吗？", function (index) {
            layer.close(index);
            $.post("/role/delete?id=" + id, function (d) {
                layer.msg(d.msg);
                if (d.code == 1) {
                    tableObj.reload();
                }
            });
        });
    }

</script>

